import json
import os
import shutil

import facade
import pymysql
from xjlibrary.our_file_dir.base_dir import BaseDir

curPath = os.path.dirname(os.path.abspath(__file__))
TopPath = os.path.abspath(os.path.dirname(os.path.dirname(os.getcwd())))
CellPath = os.path.join(TopPath, "download", "EI")
FILE_DOWNLOAD_PATH = os.path.join(CellPath, 'download', "json")

nCount = 0
ListSqls = []

configfile = BaseDir.get_new_path(curPath, "db.ini")
logger = facade.get_streamlogger()
mysqlutils = facade.MysqlUtiles(configfile, "db", logger=logger)


def getdocid(file):
    global ListSqls
    with open(file, "r", encoding="UTF-8") as f:
        contents = f.read()
    try:
        for onedate in json.loads(contents)["results"]:
            sql = "INSERT ignore INTO article (`docid`,`AccessionNumber`,`doi`) VALUES ('{docid}','{AccessionNumber}','{doi}') ON DUPLICATE KEY UPDATE AccessionNumber='{AccessionNumber}',`doi`='{doi}'".format(
                docid=onedate["doc"]["docid"], AccessionNumber=onedate["accnum"],
                doi=pymysql.escape_string(onedate["doi"]))
            # sql = "update article  "
            ListSqls.append(sql)
    except:
        print("json 错误")


# 插入数据库
def InsertIntoDbFromList():
    global nCount, ListSqls
    mysqlutils.ExeSqlListToDB(ListSqls)
    ListSqls = list()


def main():
    filenum = 0
    for filePath in BaseDir.get_dir_all_files(FILE_DOWNLOAD_PATH):
        print(filePath)
        listresult = getdocid(filePath)
        InsertIntoDbFromList()
        filenum += 1
        print(filePath)
        output = filePath.split("\\")[-3:-1]
        output = ["F:", "EI"] + output
        output = "\\".join(output)
        print(output)
        BaseDir.create_dir(output)
        shutil.move(filePath, output)

        print("完成解析文件数量{}".format(filenum))


if __name__ == "__main__":
    """
    解析上面几步下载下来的json,并存入article数据库
    """
    main()
